﻿using Customer.Domain;
using Customer.Infrstructre.Meet;
using Customer.Read.API.Applcation.Command.MeetCommand;
using MediatR;

namespace Customer.Read.API.Applcation.CommandHandler.MeetCommandHandler
{
    /// <summary>
    /// 显示财务应付
    /// </summary>
    public class SelectMeetCommandHandler:IRequestHandler<SelectMeetCommand, AccountsPage>
    {
        private readonly IAccountsRepository accountRep;

        public SelectMeetCommandHandler(IAccountsRepository accountRep)
        {
            this.accountRep = accountRep;
        }

        public async Task<AccountsPage> Handle(SelectMeetCommand request, CancellationToken cancellationToken)
        {
            AccountsPage page = new AccountsPage();
            var res = (await accountRep.GetAllAsync()).Where(x => x.IsDel == false && x.Office_Id == request.Office_Id);
            if (!string.IsNullOrEmpty(request.AccountsNumber))
            {
                res = res.Where(x => x.AccountsNumber == request.AccountsNumber);
            }
            if (!string.IsNullOrEmpty(request.AccountsName))
            {
                res = res.Where(x => x.AccountsName.Contains(request.AccountsName));
            }
            if (request.FinanceTypeId > 0)
            {
                res = res.Where(x => x.FinanceTypeId == request.FinanceTypeId);
            }
            if (!string.IsNullOrEmpty(request.Beneficiary))
            {
                res = res.Where(x => x.Beneficiary == request.Beneficiary);
            }
            if (!string.IsNullOrEmpty(request.Kdate))
            {
                res = res.Where(x => x.AddTime >=Convert.ToDateTime(request.Kdate));
            }
            if (!string.IsNullOrEmpty(request.Sdate))
            {
                res = res.Where(x => x.AddTime <= Convert.ToDateTime(request.Sdate));
            }
            if (request.ApprovalStatus > 0)
            {
                res = res.Where(x => x.ApprovalStatus == request.ApprovalStatus);
            }
            page.SumCount = res.Count();
            page.LastPage = (int)Math.Ceiling(page.SumCount * 1.0 / request.PageSize);
            page.list = res.Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            return page;






        }
    }
}
